package com.md24housecall.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;


import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.md24housecall.manager.SkypeManager;
import com.md24housecall.net.UrlBuilder;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;
import com.md24housecall.util.Utils;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.RatingBar;
import android.widget.Toast;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class RatingActivity extends Activity {
	private RatingBar ratingBar;
	private GlobalConstant constant=GlobalConstant.getInstance();
//	private String url="https://wp-dot-erudite-stratum-347.appspot.com/ws-call-tracking";
	private AQuery aQuery;
	private Utils util=Utils.getInstance();
	private SkypeManager skype=SkypeManager.getInstance();
	private int retry=0;
	
	private void updateUserStatus(final float rating){
		constant.rating=rating;
		constant.callFrom=skype.getSkypename(RatingActivity.this);
		MyLog.d(GlobalConstant.TAG,"skype name to post call tracking: "+constant.callFrom);
		Map<String, Object> params = new HashMap<String, Object>();
	    params.put("call_from", constant.callFrom);
	    params.put("call_duration", util.getDuration(System.currentTimeMillis()-constant.startTime));
	    params.put("call_time", constant.callTime);
	    params.put("call_to", constant.callTo);
	    params.put("rating", rating);
	    params.put("skype_name", constant.callFrom);
	    params.put("email_address", util.getEmailAccount(RatingActivity.this));
//	    int type=util.getPreferenceInt(constant.KEYUSERTYPE, RatingActivity.this, 0);
//	    if(type==0)
//	    	params.put("status", 0);
//	    else
//	    	params.put("status", 1);
	    String url=UrlBuilder.WS_CALL_TRACKING;
	    MyLog.d(GlobalConstant.TAG,"url post tracking: "+url);
		aQuery.ajax(url, params,JSONObject.class, new AjaxCallback<JSONObject>() {

		    @Override
		    public void callback(String url, JSONObject object,AjaxStatus status) {
		    	// TODO Auto-generated method stub
		    		MyLog.d(GlobalConstant.TAG,"retry request call fragment: "+retry);
			    	if(object==null){
						MyLog.d(GlobalConstant.TAG, "no response from server");
						if(retry<3){
							retry++;
							updateUserStatus(rating);
							return;
						}else{
							Toast.makeText(RatingActivity.this,R.string.dlg_post_tracking_error,Toast.LENGTH_LONG).show();
							retry=0;
							return;
						}
					}
			    	MyLog.d(GlobalConstant.TAG, "post call tracking: "+object.toString());
					retry=0;
					RatingActivity.this.finish();
		       }
		    });
	    
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		updateUserStatus(1);
		super.onBackPressed();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		// http://stackoverflow.com/a/5831214/190309
		// Make us non-modal, so that others can receive touch events.
		getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL,
				LayoutParams.FLAG_NOT_TOUCH_MODAL);

		// ...but notify us that it happened.
		getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
				LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);

		setContentView(R.layout.rating);
		aQuery=new AQuery(this);
		View view = this.getWindow().getDecorView();
	    view.setBackgroundColor(Color.TRANSPARENT);
	    ratingBar=(RatingBar)findViewById(R.id.ratingBar);
	    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			
			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				updateUserStatus(rating);
//				finish();
			}
		});
	}
}
